// swagger.js
import swaggerJsdoc from 'swagger-jsdoc'
import swaggerUi from 'swagger-ui-express'

const options = {
    definition: {
        openapi: '3.0.0',
        info: {
            title: 'ExpressForGaussDB API',
            version: '1.0.0',
            description: 'API 文档',
        },
        servers: [
            {
                url: process.env.NODE_ENV === "production"
                    ? process.env.SWAGGER_HOST || "http://localhost:3000"
                    : "http://localhost:3000", // 修改为你的服务地址
                description: '开发服务器',
            },
        ],
        tags: [
            {
                name: 'Users',
                description: '用户相关操作',
            },
        ],
        components: {
            schemas: {
                User: {
                    type: 'object',
                    properties: {
                        id: {
                            type: 'integer',
                            description: '用户的唯一标识符',
                        },
                        name: {
                            type: 'string',
                            description: '用户的名字',
                        },
                        email: {
                            type: 'string',
                            description: '用户的电子邮件地址',
                        },
                    },
                },
                UserCreate: {
                    type: 'object',
                    properties: {
                        name: {
                            type: 'string',
                            description: '用户的名字',
                        },
                        email: {
                            type: 'string',
                            description: '用户的电子邮件地址',
                        },
                    },
                    required: ['name', 'email'],
                },
                UserUpdate: {
                    type: 'object',
                    properties: {
                        name: {
                            type: 'string',
                            description: '用户的名字',
                        },
                        email: {
                            type: 'string',
                            description: '用户的电子邮件地址',
                        },
                    },
                },
            },
        },
    },
    apis: ['./src/routes/*.js'], // 指定包含 Swagger 注释的文件路径
};

const specs = swaggerJsdoc(options);

const swaggerDocs = (app) => {
    app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
    app.get("/openapi.json", (req, res) => {
        res.json(specs);
    });
};





export default swaggerDocs;